package com.gusto.engine.colfil.transformation.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gusto.engine.colfil.Evaluation;
import com.gusto.engine.colfil.transformation.Transformation;

/**
 * <p>The ZScore transformation normalizes the evaluations, 
 * by setting the mean of evaluations to 0 and the variation to 1.</p>
 * 
 * @author amokrane.belloui@gmail.com
 * 
 */
public class ZScoreTransformation implements Transformation {
	
	private Logger log = Logger.getLogger(getClass());
	
	private double SOMETHING = 0.00000000000001;
	
	/**
	 * {@inheritDoc}
	 */
	public List<Evaluation> transform(List<Evaluation> evals) {
		log.info("Applying Z-Score Transformation");
		double total = 0;
		double squareTotal = 0;
		double count = evals.size();
		double mean = 0;
		for (Evaluation e : evals) {
			total += e.getValue();
			squareTotal += (e.getValue() * e.getValue()) ;
		}
		mean = (total / count) + SOMETHING;
		
		List<Evaluation> res = new ArrayList<Evaluation>(); 
		for (Evaluation e : evals) {
			double norm = (e.getValue() - mean)  / (Math.sqrt(squareTotal/count));
			e.setValue(norm);
			res.add(e);
		}
		return res;
	}
	
}
